package cn.itcast.shop.product;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import cn.itcast.shop.utils.PageHibernateCallback;

public class ProductDao extends HibernateDaoSupport{

	public List<Product> findHost() {
		DetachedCriteria criteria=DetachedCriteria.forClass(Product.class);
		criteria.add(Restrictions.eq("is_hot", 1));
		return this.getHibernateTemplate().findByCriteria(criteria, 0, 10);
	}

	public List<Product> findNew() {
		// TODO Auto-generated method stub
		return this.getHibernateTemplate().executeFind(new PageHibernateCallback<Product>("from Product order by pdate desc", null, 0, 10));
	}

	public Integer findCountByCid(Integer cid) {
		List<Long> list=this.getHibernateTemplate().find("select count(*) from Product p,CategorySecond cs where p.categorySecond=cs and cs.category.cid=?",cid);
		return list.get(0).intValue();
	}

	public List<Product> findByPage(Integer cid, int begin, int limit) {
		String hql="select p from Product p ,CategorySecond cs where p.categorySecond =cs and cs.category.cid=?";
		
		List<Product> list=this.getHibernateTemplate().executeFind(new PageHibernateCallback<Product>(hql, new Object[]{cid}, begin, limit));
		
		return list;
	}

	public Product findByPid(Integer pid) {
		return this.getHibernateTemplate().get(Product.class, pid);
		
	}

	public Integer findCountByCsid(Integer csid) {
		String hql="select count(*) from Product p join p.categorySecond cs where cs.csid=?";
		List<Long> list= this.getHibernateTemplate().find(hql,csid);
		return list.get(0).intValue();
	}

	public List<Product> findByPageCsid(Integer csid, int begin, int limit) {
		String hql="select p from Product p join p.categorySecond cs where cs.csid=?";
		List<Product> list=this.getHibernateTemplate().executeFind(new PageHibernateCallback<Product>(hql, new Object[]{csid}, begin, limit));
		return list;
	}

	public Integer findCount() {
		String hql="select count(*) from Product";
		List<Long> list= this.getHibernateTemplate().find(hql);
		if(list.size()>0){
			return list.get(0).intValue();
		}
		return null;
	}

	public List<Product> findByPages(int begin, int limit) {
		String hql="from Product";
		List<Product> list=this.getHibernateTemplate().executeFind(new PageHibernateCallback<Product>(hql, null, begin, limit));
		if(list.size()>0){
			return list;
		}
		return null;
	}

	public void save(Product product) {
		this.getHibernateTemplate().save(product);
		
	}

	public void delete(Product product) {
		product=this.getHibernateTemplate().get(Product.class,product.getPid());
		this.getHibernateTemplate().delete(product);
	}

	public void update(Product product) {
		this.getHibernateTemplate().update(product);
		
	}

}
